package learning.groovy.regex

class RegularExpressions {

	static main(args){
		def twister = 'she sells sea shells at the sea shore of seychelles'
		
		// twister must contain a substring of size 3
		// that starts with s and ends with a
		assert twister =~ /s.a/
		
		def finder = (twister =~ /s.a/)
		println finder instanceof java.util.regex.Matcher
		
		def words = twister.split(/ /)
		println words.size()
		println words[0] == 'she'
		
		def myFairStringy = 'The rain in Spain stays mainly in the plain!'
		// words that end with 'ain': \b\w*ain\b
		def BOUNDS = /\b/
		def rhyme = /$BOUNDS\w*ain$BOUNDS/
		def found = ''
		myFairStringy.eachMatch(rhyme) {match ->
			found += match + ' '
		}
		println found
		
		found = ''
		(myFairStringy =~ rhyme).each {match ->
			found += match + ' '
		}
		println found
		
		//通过闭包替换规则匹配的每一个结果，变量it引用匹配的子字符串，使用下划线替换"ain"
		def cloze = myFairStringy.replaceAll(rhyme){ it-'ain'+'___'}
		println cloze
		
	}
	
}
